//------------------------------------------------------------------------------
//!	@file enum-test.c
//!	@brief
//!		C enum test
//!
//!		This test was copied from the stack overflow website. It's an
//!		excellent example of a number of things!  And so short too! The
//!		first thing is a comprehensive test of what can be done with
//!		enums in straight C.  the second is the way the table was
//!		developed using the pre-processor macro STR_CASE.
//!
//!		I have reformatted it slightly, but the core is still there.
//!
//!	$Id: enum-test.c 147 2013-03-03 06:01:03Z ac.verbeck@gmail.com $
//------------------------------------------------------------------------------

#include <stdio.h>

//
// Here's a enum typedef to encapsulate a few different fruits
//
typedef enum fruit { apple, banana, cherry, peach, grape, fruit_ct} fruit;

//
// This function converts from an enum ID to a string
//
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wswitch"
#define	STR_CASE(x)	case x:	return #x
const char * e2s_fruit(fruit f)
{
	switch (f) {
		STR_CASE(apple);
		STR_CASE(banana);
		STR_CASE(cherry);
		STR_CASE(peach);
		STR_CASE(grape);
	}
	return "out of range";
}
#undef STR_CASE
#pragma GCC diagnostic pop

//
//	I have no idea what fruit will be passed to me, but I know it is
//	a fruit and I want to print the name at runtime
//
static void	testCall(fruit f)
{
	printf("I got called with fruit %s\n", e2s_fruit(f));
}

//------------------------------------------------------------------------------
//	Main application starts here
//------------------------------------------------------------------------------
int	main(int argc, char	** argv)
{
	printf("%s\n", e2s_fruit(banana));

	fruit myFruit =	cherry;
	myFruit++;
	printf("%s\n", e2s_fruit(myFruit));


//
// I can also pass an enumeration to a function
//
	testCall(grape);

//
//	How to get the number of items in the enum
//
	printf("number of fruits in fruit type: %d\n", fruit_ct);

//
//	The bad thing about enums is that the enum
//	is compiler defined type (typically an int)
//	and can be set to any int value...
//
//	IMO, this should be an error...
//
	myFruit	= 7;
	printf("%s\n", e2s_fruit(myFruit));

	return 0;
}